/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air_hockey_v2.engine;

/**
 *
 * @author cooler
 */
public class Taco extends Itens {
    
    private int vxM, vyM;
    private String filename;
    static int i = 0;

    @Override
    public void setVxM(int vxM) {
        this.vxM = vxM;
    }

    @Override
    public void setVyM(int vyM) {
        this.vyM = vyM;
    }

    public Taco(String filename) {
        vx = 1;
        vy = 1;
        this.filename = filename;
    }

    public String getFilename() {
        return(this.filename);
    }

    @Override
    public void move(int x, int y) {
//        setX(x-23);
//        setY(y-23);
        int xTaco = this.getX();
        int yTaco = this.getY();

        while ((vx != 0 && vy != 0) && (xTaco != (x - 23) || yTaco != (y - 23))) {
            if (xTaco < (x - 23)) {
                xTaco = xTaco + vx;
                this.setX(xTaco);
            }
            if (xTaco > (x - 23)) {
                xTaco = xTaco - vx;
                this.setX(xTaco);
            }
            if (yTaco < (y - 23)) {
                yTaco = yTaco + vy;
                this.setY(yTaco);
            }
            if (yTaco > (y - 23)) {
                yTaco = yTaco - vy;
                this.setY(yTaco);
            }
        }
    }

    @Override
    public void colisao(Itens disco) {
        int xD, yD, xT, yT;
        double aux;
        xD = disco.getX() + 18;
        yD = disco.getY() + 18;
        xT = x + 23;
        yT = y + 23;
        aux = Math.sqrt(Math.pow((xD - xT), 2) + Math.pow((yD - yT), 2));        
        if (aux <= 41) {
            if (xD < (xT - 23 / 2) && (yD < (yT - 23 / 2) || yD > (yT + 23 / 2))) {
                /* Colisão com o topo esquerdo e lado inferior do taco */
                disco.setVx(disco.getVx() * -1 + vxM);
                disco.setVy(disco.getVy() * -1 + vyM);
            } else if (xD >= (xT - 23 / 2) && xD <= (xT + 23 / 2)) {
                /* Colisão com o topo e lado inferior do taco */
                disco.setVy(disco.getVy() * -1 + vyM);
            } else if (xD > (xT + 23 / 2) && (yD < (yT - 23 / 2) || yD > (yT + 23 / 2))) {
                /* Colisão com o topo direito e lado inferior direito do taco */
                disco.setVx(disco.getVx() * -1 + vxM);
                disco.setVy(disco.getVy() * -1 + vyM);
            } else {
                disco.setVx(disco.getVx() * -1 + vxM);
            }
        }
    }

    @Override
    public boolean isColisao(int x, int y, Itens disco) {

        int xD, yD, xT, yT;
        double aux;

        xD = disco.getX() + 18;
        yD = disco.getY() + 18;
        xT = x;
        yT = y;
        aux = Math.sqrt(Math.pow((xD - xT), 2) + Math.pow((yD - yT), 2));
        if (aux < 41) {
            return true;
        }
        return false;
    }

    @Override
    public void melhorXY(int x, int y, Itens disco) {

        int xD, yD, xT, yT;
        double aux;

        xD = disco.getX() + 18;
        yD = disco.getY() + 18;
        xT = getX() + 23;
        yT = getY() + 23;

        aux = Math.sqrt(Math.pow((xD - xT), 2) + Math.pow((yD - yT), 2));

        while ((vx != 0 && vy != 0) && aux >= 41) {
            this.setX(xT - 23);
            this.setY(yT - 23);
            if (xT < x) {
                xT = xT + getVx();
            }
            if (xT > x) {
                xT = xT - getVx();
            }
            if (yT < y) {
                yT = yT + getVy();
            }
            if (yT > y) {
                yT = yT - getVy();
            }

            aux = Math.sqrt(Math.pow((xD - xT), 2) + Math.pow((yD - yT), 2));
        }
    }
}
